// 售后单详情页
const app = getApp();
import { handleAfterSaleOrderStatus } from '../../biztools/order';
import { getAfterSaleOrderDetail, cancelAfterSaleOrder, addExpressInfo } from '../../api/order';
import { getExpressCompanyList } from '../../../api/common';
import { ORDER_OWNER_TYPE } from '../../../constants/order';
import { AfterSaleOrderDetailMixins } from '../../../behaviors/page';
const { showLoading, hideLoading, tipError } = require('../../../common/env');

Page({
    mixins: [AfterSaleOrderDetailMixins],
    data: {
        orderOwnerType: ORDER_OWNER_TYPE,
        afterSaleOrderDetail: {},
        afterSaleOrderStatusInfo: {},
        expressId: '',
        expressName: '',
        expressNumber: '',
        showSelectExpress: false,
        expressList: [],
    },

    onLoad: async function (options) {
        if (!app.globalData.isReadyAuth) {
            const _ = this;
            const originFunc = app.userIdentifyCallback;
            app.userIdentifyCallback = function (...args) {
                _.onLoad(...args);
                return originFunc && originFunc(...args);
            }
            return;
        } else {
            const { orderNumber } = options;
            // 获取售后点详情
            await this.refreshAfterOrderDetail(orderNumber);
        }
    },

    // 获取售后单详情
    refreshAfterOrderDetail: async function (orderNumber) {
        showLoading('加载中...');
        const afterSaleOrderDetail = await getAfterSaleOrderDetail(orderNumber);
        this.setData({
            afterSaleOrderDetail,
            afterSaleOrderStatusInfo: handleAfterSaleOrderStatus(afterSaleOrderDetail),
        });
        hideLoading();
    },
    // 取消售后单
    cancelAfterOrder: async function () {
        const { serialNumber } = this.data.afterSaleOrderDetail;
        const flag = await cancelAfterSaleOrder(serialNumber);
        console.log('AfterSaleDetail => 取消售后单结果', flag);
        if (flag) {
            await this.refreshAfterOrderDetail(serialNumber);
        }
    },

    // 选择快递公司
    selectExpress: async function () {
        // 获取快递公司列表
        const expressList = await getExpressCompanyList();
        this.setData({
            expressList,
            showSelectExpress: true,
        });
    },

    expressValueChange: function (event) {
        const { name, id } = event.detail;
        this.setData({
            expressId: id,
            expressName: name,
        });
    },

    // 快递单号改变
    expressNumberChange: function (event) {
        console.log('AfterSaleDetail => 快递单号改变', event.detail);
        this.setData({
            expressNumber: event.detail,
        });
    },
    // 确认快递信息
    ensureExpressAction: async function () {
        const { expressId, expressNumber, expressName, afterSaleOrderDetail } = this.data;
        const { serialNumber } = afterSaleOrderDetail;
        if (!expressId || !expressNumber) {
            tipError('快递信息不完整');
            return;
        }
        const param = {
            platformId: expressId,
            platformName: expressName,
            platformNo: expressNumber,
        };
        const flag = await addExpressInfo(serialNumber, param);
        console.log('AfterSaleDetail => 售后单添加快递信息', flag);
        if (flag) {
            await this.refreshAfterOrderDetail(serialNumber);
        }
    },
})